package adrian.sunlight.ws.axis2;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Clasa, colectie de metode statice pentru a usura lucrul cu variabile care reprezinta 
 * date sau intervale de timp
 * 
 * @author Adrian #350;ilimon
 * @version 1.0
 */
public class TheTime {

	public static int TIME_UNIT_YEARS 	= 1;
	public static int TIME_UNIT_MONTHS 	= 2;
	public static int TIME_UNIT_WEEKS 	= 3;
	public static int TIME_UNIT_DAYS 	= 4;
	public static int TIME_UNIT_HOURS 	= 5;
	public static int TIME_UNIT_MINUTES = 6;
	public static int TIME_UNIT_SECONDS = 7;
	
	public static String format = "yyyy-MM-dd HH:mm:ss"; 
	
	/**
	 * returneaza data si timpul curent formatate dupa String-ul parametru
	 * @param format
	 * @return String
	 */
	public static String now(String format){
		DateFormat dateFormat = new SimpleDateFormat(format);
        Date date = new Date();
        return dateFormat.format(date);
	}
	
	public static String now(){
		return TheTime.now(TheTime.format);
	}
	
	public static void setFormat(String newFormat){
		TheTime.format = newFormat;
	}
	
	//YYYY-MM-DD H:i:s
	public static float difference(String start_date, String end_date, int units){
		String[] DArray;
		String[] datePart;
		String[] timePart;
		
		Calendar calendarOne = Calendar.getInstance();
		Calendar calendarTwo = Calendar.getInstance();
		
		DArray = start_date.split(" ");
		datePart = DArray[0].split("-");
		timePart = DArray[1].split(":");
		calendarOne.set(Integer.parseInt(datePart[0]), Integer.parseInt(datePart[1]), Integer.parseInt(datePart[2]), 
				Integer.parseInt(timePart[0]), Integer.parseInt(timePart[1]), Integer.parseInt(timePart[2])
		);
		
		DArray 		= end_date.split(" ");
		datePart	= DArray[0].split("-");
		timePart	= DArray[1].split(":");
		calendarTwo.set(Integer.parseInt(datePart[0]), Integer.parseInt(datePart[1]), Integer.parseInt(datePart[2]), 
				Integer.parseInt(timePart[0]), Integer.parseInt(timePart[1]), Integer.parseInt(timePart[2])
		);
		
		long  diffInMiliseconds = calendarTwo.getTimeInMillis() - calendarOne.getTimeInMillis();
		float diffInSeconds		= diffInMiliseconds / 1000;
		
		if(units == TheTime.TIME_UNIT_SECONDS){
			return diffInSeconds;
		}
		
		if(units == TheTime.TIME_UNIT_MINUTES){
			return diffInSeconds / 60;
		}
		
		if(units == TheTime.TIME_UNIT_HOURS){
			return diffInSeconds / 3600;
		}
		
		if(units == TheTime.TIME_UNIT_DAYS){
			return diffInSeconds / 86400;
		}
		
		if(units == TheTime.TIME_UNIT_MONTHS){	
			return diffInSeconds / 2628000;
		}
		
		if(units == TheTime.TIME_UNIT_WEEKS){
			return diffInSeconds / (86400 * 7);
		}
		
		if(units == TheTime.TIME_UNIT_YEARS){
			return diffInSeconds / 31536000;
		}
		
		return 0;
	}
	
}
